package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.supervision.convert.JzfaManageConvert;
import com.engine4cloud.tdf.supervision.dto.create.JzfaManageCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzfaManageQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzfaManageResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzfaManageUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzfaManageEntity;
import com.engine4cloud.tdf.supervision.mapper.JzfaManageMapper;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.JzfaManageService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 矫正方案管理
 *
 * @author wenjie
 * @date 2024-04-19 09:45:15
 */
@Service
public class JzfaManageServiceImpl extends ServiceImpl<JzfaManageMapper, JzfaManageEntity> implements JzfaManageService {
    @Resource
    private JzdxService jzdxService;

    @Override
    public PageInfo<JzfaManageResultDTO> getPageList(Params pageable, JzfaManageQueryDTO dto) {
        IPage<JzfaManageResultDTO> page = this.baseMapper.pageList(Pages.page(pageable), dto);
        return Pages.convert(page);
    }

    @Override
    public JzfaManageResultDTO getById(Long id) {
        return JzfaManageConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    public Boolean create(JzfaManageCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        return super.save(JzfaManageConvert.INSTANCE.create(dto, dx.getTenantId()));
    }

    @Override
    public Boolean update(JzfaManageUpdateDTO dto) {
        Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        return super.updateById(JzfaManageConvert.INSTANCE.update(dto));
    }
}